﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using YiSha.Entity.Test;
using YiSha.Model.Param.Test;
using YiSha.Model.Result.PMCManage;
using YiSha.Service.Test;
using YiSha.Util.Extension;

namespace YiSha.Business.Test
{
    public class ScheduleBLL
    {

        private readonly ScheduleService scheduleService = new ScheduleService();

        private readonly ProcessService processService = new ProcessService();

        #region 获取数据
        public async Task<List<ScheduleEntity>> GetListJson(ScheduleParam scheduleParam)
        {

            return await scheduleService.GetListJson(scheduleParam);
        }

        //参数分别为日期、部门备注
        public async Task<List<WorkOrderNumberDayInfo>> GetDayPanel(DateTime param,String departmentRemark)
        {

            ScheduleParam scheduleParam = new ScheduleParam
            {
                ProcessIds = new List<long?>() // 初始化List
            };

            List<WorkOrderNumberDayInfo> result = new List<WorkOrderNumberDayInfo>();
            ProcessParam processParam = new ProcessParam
            {
                DepartmentRemark = departmentRemark,
                IsTop = true
            };
            List<ProcessEntity> processEntities = await processService.GetListJson(processParam);//查询出有权限的顶级工序

            for (var i=0;i<processEntities.Count;i++)
            {
                ProcessParam childrenProcessParam = new ProcessParam
                {
                    DepartmentRemark = departmentRemark,
                    ParentId = processEntities[i].Id,
                    ParentIdIsEqual = true
                };
                List<ProcessEntity> children = await processService.GetListJson(childrenProcessParam);
                for (var j=0;j<children.Count;j++)
                {
                    scheduleParam.ProcessIds.Add(children[j].Id);
                }
                processEntities[i].Children = children;
            }//将子工序一并查出，并保持树形结构

            //后期将追加工令的筛选条件

            //获取到排产计划
            List<ScheduleEntity> scheduleEntities = await scheduleService.GetListJson(scheduleParam);

            List<string> workOrderNumbers = scheduleEntities
                .Select(s => s.WorkOrderNumber)
                .Where(w => !string.IsNullOrEmpty(w)) // 过滤掉null和空字符串
                .Distinct()
                .ToList();//获取所有工令号
            
            for(int i=0;i<workOrderNumbers.Count;i++)
            {
                WorkOrderNumberDayInfo workOrderNumberDayInfo = new WorkOrderNumberDayInfo//为每个工令日报信息追加基础信息。
                {
                    WorkOrderNumber = workOrderNumbers[i],
                    Processes = new List<ProcessDayInfo>()//初始化该工令内的当日工序完成情况
                };
                //此处追加其他基础信息，实际应用中从MES拉取，在此暂时使用模拟数据。
                workOrderNumberDayInfo.CustomerOrderNumber = "模拟客户订单号";
                workOrderNumberDayInfo.ProductNumber = "模拟产品编号";
                workOrderNumberDayInfo.Customer = "模拟客户";
                workOrderNumberDayInfo.ProjectName = "模拟项目名称";

                for (int j=0;j<processEntities.Count;j++)//遍历一级工序
                {
                    ProcessDayInfo processDayInfo = new ProcessDayInfo
                    {
                        Name = processEntities[j].Name,
                        Children = new List<ProcessDayInfoChildren>()//初始化子工序
                    };
                    workOrderNumberDayInfo.Processes.Add(processDayInfo);
                    for (int k = 0; k < processEntities[j].Children.Count;k++)//遍历子工序
                    {
                        ProcessDayInfoChildren processDayInfoChildren = new ProcessDayInfoChildren
                        {
                            Name = processEntities[j].Children[k].Name
                        };

                        ScheduleEntity todaySchedule = scheduleEntities.FirstOrDefault(s => s.Date.Date == param.Date &&
                            s.ProcessId == processEntities[j].Children[k].Id &&
                            s.WorkOrderNumber == workOrderNumbers[i]);//查找该工令该工序当天的排产计划（可能为空）
                        if (todaySchedule.IsEmpty())
                        {
                            processDayInfoChildren.Remark = "该工序当天无排产计划";
                        } else
                        {
                            processDayInfoChildren.TodayPlan = todaySchedule.PlanNumber;
                            processDayInfoChildren.TodayActual= todaySchedule.RealNumber;
                            processDayInfoChildren.Remark = todaySchedule.Remark;
                        }

                        workOrderNumberDayInfo.Processes[j].Children.Add(processDayInfoChildren);//将整理好的二级工序返回
                    }
                }

                result.Add(workOrderNumberDayInfo);

            }

            return result;
        }
        #endregion

        #region 提交数据
        public async Task<ScheduleEntity> Insert(ScheduleEntity entity)
        {

            return await scheduleService.Insert(entity);
        }

        public async Task<ScheduleEntity> Update(ScheduleEntity entity)
        {
            return await scheduleService.Update(entity);
        }
        #endregion

    }
}
